package com.edu.internship.util;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;

/**
 * code is far away from bug with the animal protecting
 *
 * @author lsq28657638@gmail.com
 * @description
 * @date 2019-12-23
 */
public class ValidatorUtils {

    private static final Pattern USERNAME_PATTERN = Pattern.compile("[a-z][a-z0-9_]{7,19}");
    private static final Pattern MOBILE_PATTERN = Pattern.compile("(1[345789])\\d{9}");
    private static final Pattern EMAIL_PATTERN = Pattern.compile("\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}");
    private static final Pattern PWD_PATTERN = Pattern.compile("(?=.*[A-Za-z])(?=.*\\d)(?=.*[$@!%*#?&])[A-Za-z\\d$@!%*#?&]{3,}");

    private ValidatorUtils() {
    }

    public static boolean isUsername(String username) {
        if (StringUtils.isBlank(username)) {
            return false;
        }
        return USERNAME_PATTERN.matcher(username).matches();
    }

    public static boolean isMobile(String mobile) {
        if (StringUtils.isBlank(mobile)) {
            return false;
        }
        return MOBILE_PATTERN.matcher(mobile).matches();
    }

    public static boolean isEmail(String email) {
        if (StringUtils.isBlank(email)) {
            return false;
        }
        return EMAIL_PATTERN.matcher(email).matches();
    }

    public static boolean pwdIsSatisfy(String pwd) {
        if (StringUtils.isBlank(pwd)) {
            return false;
        }
        return PWD_PATTERN.matcher(pwd).matches();
    }
}
